////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.orders.windows.objects {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	
	import kiwi.components.Button;
	import kiwi.components.DragObject;
	import kiwi.components.IDropTarget;
	import kiwi.events.CallbackEvent;
	import kiwi.utils.Validation;
	
	import plantool.display.skins.EmptySkin;
	import plantool.display.windows.BaseWindow;
	import plantool.display.windows.objects.WindowRow;
	import plantool.helpers.TextFieldHelper;
	import plantool.modules.orders.data.Order;
	import plantool.modules.orders.data.Orderrow;
	import plantool.modules.workplaces.display.IWorkplaceDay;
	import plantool.modules.workplaces.display.WorkplaceDay;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class OrderListWindowOrderRow extends WindowRow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _orderRow:Orderrow			= null;
		private var _order:Order				= null;
		private var _tf:TextField				= null;
		private var _editBtn:Button				= null;
		private var _delBtn:Button				= null;
		private var _planningDrag:DragObject 	= null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get orderRow():Orderrow 			{ return _orderRow; }
		public function set orderRow(value:Orderrow):void 	{ _orderRow = value; invalidate(); }
		public function get order():Order		 		{ return _order; }
		public function set order(value:Order):void		{ _order = value; }
		
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function OrderListWindowOrderRow(window:BaseWindow) {
			super(window);
		}
		
		public override function dispose():void {
			//unset user
			_orderRow 	= null;
			
			//remove textfield
			if (_tf) {
				if (contains(_tf)) {
					removeChild(_tf);
				}
				_tf		= null;
			}
			
			//dispose buttons
			if (_editBtn) {
				_editBtn.dispose();
				_editBtn = null;
			}
			if (_delBtn) {
				_delBtn.dispose();
				_delBtn = null;
			}
			
			if (_planningDrag) {
				_planningDrag.dispose();
				_planningDrag = null;
			}
			
			//super dispose
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function mouseHandler_delBtn(e:CallbackEvent):void {
		}
		
		private function onDropped_planning(target:IDropTarget, item:DragObject):void {
			orderApi.planOrderrow(_orderRow, (target as IWorkplaceDay).date, (target as IWorkplaceDay).workplace.id, null, null);
		}
		
		private function onValidateDrop_planning(target:IDropTarget, item:DragObject):Boolean {
			return true;
		}
		
		protected override function init():void {
			//create label
			_tf 	= TextFieldHelper.createInfoTf(TextFieldAutoSize.LEFT, TextFormatAlign.LEFT, false, false, 0);
			addChild(_tf);
			
			//create buttons
			_editBtn 		= new Button();
			_editBtn.width 	= 10;
			_editBtn.height = 10;
			_editBtn.x		= width - 30;
			_editBtn.y		= (height - 10) / 2;
			addChild(_editBtn);
			
			_delBtn 		= new Button();
			_delBtn.width 	= 10;
			_delBtn.height 	= 10;
			_delBtn.x		= width - 15;
			_delBtn.y		= (height - 10) / 2;
			_delBtn.mouseHandler = mouseHandler_delBtn;
			addChild(_delBtn);
			
			//create drag item
			if (_orderRow.remaining > 0) {
				_planningDrag 			= new DragObject();
				_planningDrag.skin		= new EmptySkin();
				_planningDrag.dragItem 	= this;
				_planningDrag.dropTargets.push(WorkplaceDay);
				_planningDrag.width		= _editBtn.x - 10;
				_planningDrag.height	= this.height;
				_planningDrag.droppedHandler 		= onDropped_planning;
				_planningDrag.validateDropHandler 	= onValidateDrop_planning;
				addChild(_planningDrag);
			}
			
			super.init();
		}
		
		public override function draw():void {
			//update textfield
			if (Validation.isSet(_tf)) {
				//textfield position
				_tf.x 	= 8;
				_tf.y	= (height - _tf.height) / 2;
				
				//show username
				if (Validation.isSet(_orderRow)) {
					_tf.text = _order.meta.string_field_1 + " " + _order.onr.toString() + " " + _orderRow.time.toString() + " " +  _orderRow.remaining.toString();
				}
			}
			
			//super draw
			super.draw();
		}
	}
}